package com.rla.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.rla.common.annotation.Anonymous;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.SecurityUtils;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.domain.bo.VipUserInfoBo;
import com.rla.domain.vo.VipUserInfoVo;
import com.rla.service.IVipUserInfoService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.enums.BusinessType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;


/**
 * 小程序用户信息
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shop/user/info")
public class VipUserInfoController extends BaseController {

    private final IVipUserInfoService iVipUserInfoService;

    /**
     * 查询小程序用户信息列表
     */
    @GetMapping("/list")
    public TableDataInfo<VipUserInfoVo> list(VipUserInfoBo bo, PageQuery pageQuery) {
        return iVipUserInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小程序用户信息列表
     */
    @Log(title = "小程序用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VipUserInfoBo bo, HttpServletResponse response) {
        List<VipUserInfoVo> list = iVipUserInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序用户信息", VipUserInfoVo.class, response);
    }

    /**
     * 获取小程序用户信息详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<VipUserInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iVipUserInfoService.queryById(id));
    }

    /**
     * 新增小程序用户信息
     */
    @Log(title = "小程序用户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VipUserInfoBo bo) {
        return toAjax(iVipUserInfoService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改小程序用户信息
     */
    @Log(title = "小程序用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VipUserInfoBo bo) {
        return toAjax(iVipUserInfoService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除小程序用户信息
     *
     * @param ids 主键串
     */
    @Log(title = "小程序用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iVipUserInfoService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    /*
     *  登录成功后获取app用户信息
     * */
    @GetMapping("/getVipUserInfo")
    public R<Map<String, Object>> getInfo() {
        Map<String, Object> ajax = new HashMap<>();
        String personnelId = SecurityUtils.getUserId().toString();
        VipUserInfoVo vo = iVipUserInfoService.queryById(personnelId);
        ajax.put("personnel", vo);
        return R.ok(ajax);
    }
}
